package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Contractor;
import com.init.engineering.pojo.Developer;
import com.init.engineering.pojo.Project;
import com.init.engineering.service.ContractorService;
import com.init.engineering.service.ProjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/contractor")
public class ContractorController {

    @Autowired
    private ContractorService contractorService;

    @Autowired
    private ProjectService projectService;

    /**
     * 查询所有承包商
     * @return
     */
    @RequestMapping("/findContractors")
    public Result findContractors(){
        List<Contractor> contractors = contractorService.findContractors();
        Result result = new Result();
        if(contractors != null){
            result.setFlag(true);
            result.setData(contractors);
        }else{
            result.setFlag(false);
        }
        return result;
    }

    /**
     * 通过开发商ID查找开发商
     * @return
     */
    @RequestMapping("/findIDs")
    public Result findContractorsDid(){
        List<Contractor> contractors = contractorService.findContractors();
        List<String> cids = new ArrayList<String>();
        Result result = new Result();
        if(contractors != null){
            for(int i = 0;i < contractors.size();i++){
                cids.add(contractors.get(i).getCid());
            }
            result.setData(cids);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }


    /**
     * 查询所有开发商ID
     * @return
     */
    @RequestMapping("/findNames")
    public Result findContractorsDname(){
        List<Contractor> contractors = contractorService.findContractors();
        List<String> cnames = new ArrayList<String>();
        Result result = new Result();
        if(contractors != null){
            for(int i = 0;i < contractors.size();i++){
                cnames.add(contractors.get(i).getCname());
            }
            result.setData(cnames);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }


    /**
     * 分页查询所有承包商
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/all")
    public PageResult findContractorsByPages(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = contractorService.findContractorsByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除承包商
     * @param cid
     * @return
     */
    @GetMapping("/delete/{cid}")
    @ApiOperation(value = "删除承包商",notes = "删除接口",httpMethod = "get")
    public Result deleteContractor(@PathVariable("cid") String cid){
        int row = contractorService.deleteContractor(cid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("承包商删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("承包商删除失败，请重试");
        }
        List<Project> projects = projectService.findProjectsByCid(cid);
        //判断承包商是否存在对应工程
        if(projects != null){
            for(int i = 0;i < projects.size();i++){
                Project project = projects.get(i);
                project.setCid("");
                projectService.updateProject(project);
            }
        }
        return result;
    }


    /**
     * 新增承包商
     * @RequestBody 来接收前端传递给后端的json字符串中的数据,将该json数据对象转为java对象
     * @param contractor
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增承包商",notes = "添加接口",httpMethod = "post")
    public Result addProject(@RequestBody Contractor contractor){
        //获取系统更新时间
        contractor.setUpdatetime(new Date());
        int row = contractorService.saveContractor(contractor);
        Result result = new Result();
        if(row > 0){
            result.setMessage("承包商添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("承包商添加失败，请重试");
        }
        return result;
    }
}
